package controller;

import java.security.InvalidParameterException;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.remotecontrol.R;

import common.DimensionHelper;
import components.FragmentMediator;
import components.KeyboardManager;
import components.PasscodeManager;
import components.SMSCommandManager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class SetPasscodeFragment extends SherlockFragment implements OnClickListener {
	EditText mPassword;
	EditText mConfirmPassword;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.set_passcode_fragment, container, false);

		view.findViewById(R.id.set_passcode_btn_save).setOnClickListener(this);
		
		mPassword = ((EditText) view.findViewById(R.id.set_passcode_edit_passcode));
		mConfirmPassword = ((EditText) view.findViewById(R.id.set_passcode_edit_confirm_passcode));
		
		mPassword.requestFocus();
		InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		imgr.showSoftInput(mPassword, 0);
		//InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		imgr.showSoftInput(mPassword, 0);
		KeyboardManager.showKeyboard(mPassword, this.getActivity());
		
		return view;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.set_passcode_btn_save:
			//TODO: Save phone number
			PasscodeManager passcodeManager = PasscodeManager.sharedPasscode(getActivity());
			
			
			String text = ((EditText) getView().findViewById(R.id.set_passcode_edit_passcode)).getText().toString();
			String text2 = ((EditText) getView().findViewById(R.id.set_passcode_edit_confirm_passcode)).getText().toString();
			
			if (!text.equals(text2)) {
				Toast.makeText(getActivity(), getActivity().getString(R.string.passcode_is_not_match), Toast.LENGTH_SHORT).show();
				return;
			}
			
			try {
				PasscodeManager.sharedPasscode(getActivity()).setPasscode(getActivity(), text);
			} catch (InvalidParameterException e) {
				// TODO: handle exception
			} catch (NumberFormatException e) {
				final AlertDialog alert = new AlertDialog.Builder(getActivity()).create();
				alert.setTitle(R.string.alert_title);
				alert.setMessage(getActivity().getString(R.string.message_wrong_passcode));
				alert.setButton(getActivity().getString(R.string.OK), new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(final DialogInterface dialog, final int which) {
						// TODO Auto-generated method stub
						alert.dismiss();
					}
				});
				alert.show();
				return;
			}
			// back
			Toast.makeText(getActivity(), getActivity().getString(R.string.change_passcode_is_successful), Toast.LENGTH_SHORT).show();
			FragmentMediator.backView(this);
			
			
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		KeyboardManager.hideKeyboard(mConfirmPassword, getActivity());
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getView().findViewById(R.id.root_layout).setPadding(
				0, DimensionHelper.getPxFromDp(getActivity(), 100), 0, 0);
	}
}
